﻿using System.Windows;


// TODO Documentation
// Thanks to www.edupics.com (metronome image)
//		Audacity (Click sounds)

namespace NMetronomeWPF
{
	using NMetronome;
	using WindowsSound;
	using System.Diagnostics;
	using System;
	using System.Windows.Documents;
	using System.Windows.Controls;
	using System.Windows.Data;



	public enum PatternEnum
	{
		High = -1,
		Low = 0,
		One,
		Two,
		Three,
		Four,
		Five,
		Six,
		Seven
	}

	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{

		public MainWindow()
		{
			InitializeComponent();
		}

		private MetronomeTicker  metronome;
		private SystemMedia beeper;

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			beeper = new SystemMedia();
			metronome = new MetronomeTicker(beeper);

			this.numericUpDown1.Value = 90;
			this.slider1.Value = 5;
			this.radioOff.IsChecked = true;

			metronome.BeepPattern = 0;
			radPatAlwaysLow.IsChecked = true;

			this.numericUpDown1.ValueChanged += numericUpDown1_ValueChanged;

		}

		private void radioOn_Checked(object sender, RoutedEventArgs e)
		{
			metronome.Start();
		}

		private void radioOff_Checked(object sender, RoutedEventArgs e)
		{
			metronome.Stop();
		}

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			//metronome.Stop();
			metronome.Dispose();
			metronome = null;
		}

		private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			Debug.WriteLine(this.slider1.Value);

			if (this.numericUpDown1 != null)
			{
				this.numericUpDown1.Value = (decimal)(15 + this.slider1.Value * 15);
			}

			metronome.BPM = (int)this.numericUpDown1.Value;
		}

		private void numericUpDown1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<decimal> e)
		{
			Debug.WriteLine(this.numericUpDown1.Value);

			if (this.slider1 != null)
			{
				this.slider1.Value = (double)((this.numericUpDown1.Value - 15) / 15);
			}

			metronome.BPM = (int)this.numericUpDown1.Value;
		}

		private void miAbout_Click(object sender, RoutedEventArgs e)
		{
			// TODO
			WPFAboutBox1 about = new WPFAboutBox1(this);
			about.ShowDialog();
		}

		private void radPat_Checked(object sender, RoutedEventArgs e)
		{
			if (sender == this.radPatAlwaysHigh)
				metronome.BeepPattern = -1;
			else if (sender == this.radPatAlwaysLow)
				metronome.BeepPattern = 0;
			else if (sender == this.radPat1)
				metronome.BeepPattern = 1;
			else if (sender == this.radPat2)
				metronome.BeepPattern = 2;
			else if (sender == this.radPat3)
				metronome.BeepPattern = 3;
			else if (sender == this.radPat4)
				metronome.BeepPattern = 4;
			else if (sender == this.radPat5)
				metronome.BeepPattern = 5;
			else if (sender == this.radPat6)
				metronome.BeepPattern = 6;
			else if (sender == this.radPat7)
				metronome.BeepPattern = 7;
		}

	}
}
